<template>
  <div class="page">
    <!-- <h3>注册</h3> -->
    <div class="header"><my-back></my-back></div>
    <div class="register">
      <section>
        <div class="top">
          <img src="../../static/images/login/logo.png" alt="" />
          <p>注册账号</p>
        </div>
        <div class="form">
          <div>
            <i></i>
            <input v-model="formData.phone" type="text" placeholder="请输入您的账号/邮箱/手机号" />
          </div>

          <div>
            <i class="pwd"></i>
            <input v-model="formData.password" type="password" placeholder="请输入密码" />
          </div>

          <div class="opt">
            <div class="l"><input type="checkbox" /> 记住账号</div>
            <div class="r">
              <router-link to="/findpwd">忘记密码?</router-link>
            </div>
          </div>
          <div>
            <button :class="formData.phone ? 'active':''" @click="registerFn">注册</button>
          </div>
        </div>
        <router-link to="/login">去登录</router-link>
      </section>
    </div>
  </div>
</template>


<style scoped lang="less">
@import "@/static/less/variable.less";
.page {
  padding: @pad;
}
.register {
  .top {
    text-align: center;
    margin-top: 50px;
    img {
      width: 80px;
    }
    p {
      margin-top: 20px;
    }
  }
  .form {
    margin-top: 40px;
    width: 100%;
    > div {
      width: 100%;
      margin-bottom: 20px;
      position: relative;
      height: 56px;
      input[type="text"] , input[type='password']{
        width: 100%;
        height: 56px;
        padding-left: 44px;
        font-size: 16px;
        box-sizing: border-box;
        border-radius: 10px;
      }
      i {
        position: absolute;
        top: 15px;
        left: 10px;
        width: 28px;
        height: 26px;
        background-image: url(../../static/images/login/message.png);
        background-size: 100% 100%;
      }
      i.pwd {
        background-image: url(../../static/images/login/lock.png);
      }

      button {
        height: 56px;
        width: 100%;
        background-color: @bgColor;
        color: #fff;
        border-radius: 10px;
        border-color: @bgColor;
        opacity: 0.6;
      }
      .active {
        opacity: 1;
      }

      
    }

    .opt {
        width: 100%;
        height: 30px;
        display: flex;
        // background-color: red;
        justify-content: space-between;
        align-items: center;
        input[type="checkbox"] {
          width: 20px;
          height: 20px;
          vertical-align: middle;
        }
        a {
           color: #000;
        }
      }
  }
}
h3 {
  font-weight: normal;
}
</style>
<script>
import MyBack from "@/components/MyBack.vue";
// 导入网络接口
export default {
  components: {
    MyBack,
  },
  data(){
    return {
      formData: {
        phone: '',
        password: ''
      }
    }
  },
  methods: {
    // 注册(假的)
    registerFn(){
        let {phone , password} = this.formData;
        console.log({phone , password})
        if(phone.length == 0 || password.length == 0) {
          alert('输入框不能为空');//以后可以自定义弹窗
          return ;
        }

        // 首先从本地获取储存的数据
        let user_str = localStorage.getItem('_USER')
        let arr_str = localStorage.getItem('_ARR')

        if(arr_str == null) {
            this.formData.id = 'xxxx'+(Math.random().toString().slice(2))
            // 把用户信息储存在本地
            let str = JSON.stringify(this.formData);
            localStorage.setItem('_USER', str);
            let arr = [];//这个数组记录所有用户
            arr.push(this.formData);
            let arr_str  = JSON.stringify(arr);
            localStorage.setItem('_ARR', arr_str);
            // 假的反馈
            alert('注册成功');
        }
        else {
            let arr = JSON.parse(arr_str);
            let success = false;
            // 循环数组
            arr.forEach(user => {
                if(user['phone'] == phone){
                   success = true;
                }
            });

            //根据布尔值判断用户是否已经存在
            if(success) {
              alert('账号已经存在')
            }
            else {
                 this.formData.id = 'xxxx' +(Math.random().toString().slice(2));
                  // 把用户信息储存在本地
                  let str = JSON.stringify(this.formData);
                  localStorage.setItem('_USER', str);
                  arr.push(this.formData);
                  let arr_str  = JSON.stringify(arr);
                  localStorage.setItem('_ARR', arr_str);
                  // 假的反馈
                  alert('注册成功');
            }
        }

        this.$router.push({
          path: '/login'
        })
    }
  }
};
</script>